import type { IColumnsOption } from 'vc';
import { CellRenderMap, INDEX, COMPANY_NAME, BUSINESS_DATA, REGISTERED_CAPITAL, PROVINCE, CITY, COUNTRY, INDUSTRY_CATEGORY, INFO_SOURCE } from '@/utils/tableColumn';
import { setHrefUrlTool } from '@/utils';

export function useTable() {
  const columns = ref<IColumnsOption[]>([
    { ...INDEX },
    { ...COMPANY_NAME },
    {
      field: 'dischargePermitNumber',
      title: '排污许可证编号',
      width: 230,
      sortable: true,
      showOverflow: 'ellipsis',
      cellRender: {
        name: CellRenderMap.HighLight
      }
    },
    {
      field: 'latelyReport',
      title: '执行报告',
      width: 160,
      slots: {
        default: ({ row }) => {
          if (row?.latelyReport?.fileUrl) {
            return <a onClick={() => setHrefUrlTool(row.latelyReport?.fileType, row.latelyReport.fileUrl)} v-html={row.latelyReport?.reportTitle}></a>;
          } else {
            return <span v-html={row.latelyReport?.reportTitle || '-'}></span>;
          }
        }
      }
    },
    {
      title: '更多执行报告',
      field: 'reports',
      width: 110,
      align: 'center',
      slots: {
        default({ row }) {
          return row?.reports?.length ? (
            <>
              <a class="details" onClick={() => openDetailModal(row.reports)}>
                更多 {row?.reports?.length}
              </a>
            </>
          ) : (
            '-'
          );
        }
      }
    },
    {
      width: 100,
      title: '报告类型',
      field: 'reportType',
      align: 'center',
      // sortable: true,
      formatter({ row }) {
        return row?.latelyReport?.reportType ?? '-';
      }
    },
    {
      width: 110,
      title: '报告日期',
      field: 'noticeDate',
      align: 'center',
      // sortable: true,
      slots: {
        default({ row }) {
          return <span v-html={ row?.latelyReport?.noticeDate ?? '-' }></span>;
        }
      }
    },
    {
      width: 220,
      title: '生产经营场所',
      field: 'productionSite',
      showOverflow: 'title'
    },
    { ...PROVINCE },
    { ...CITY },
    { ...COUNTRY },
    { ...INDUSTRY_CATEGORY },
    { ...BUSINESS_DATA },
    { ...REGISTERED_CAPITAL }
  ]);

  const detailsColumn = ref<IColumnsOption[]>([
    { ...INDEX },
    {
      title: '报告类型',
      field: 'reportType',
      width: 120,
      align: 'center',
      sortable: true
    },
    {
      title: '报告日期',
      field: 'noticeDate',
      width: 120,
      align: 'center',
      sortable: true
    },
    {
      title: '执行报告',
      field: 'reportTitle',
      minWidth: 150,
      headerAlign:'center',
      slots: {
        default: ({ row }) => {
          if (row?.fileUrl) {
            return <a onClick={() => setHrefUrlTool(row.fileType, row.fileUrl)} v-html={row?.reportTitle}></a>;
          } else {
            return <span v-html={row?.reportTitle || '-'}></span>;
          }
        }
      }
    }
  ]);
  const showDetailModal = ref(false);
  const detailsTableData = ref<any>([]);
  const openDetailModal = (data) => {
    detailsTableData.value = data;
    showDetailModal.value = true;
  };

  return {
    columns,
    showDetailModal,
    detailsColumn,
    detailsTableData
  };
}
